<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>权限管理系统</title>
    <jsp:include page="/views/include.jsp"></jsp:include>
    <script type="text/javascript">
    	var contextPath = '<%=request.getContextPath()%>';
    	$.parser.onComplete = function(){
        	$('body').css('visibility','visible');
        	setTimeout(function(){
         	$('#loading-mask').remove();
        	},50);
    	};
    	$(function(){
        	$(window).resize(function(){
            	$('#mainlayout').layout('resize');
        	});
    	});
    </script>
	<script type="text/javascript">
	//----------------------------------------------用来格式化显示状态，是否打开和是否叶子
	var status1;
	$.getJSON("<c:url value='/permissions/outDicJsonByNicknameResources.tg?nickName=status'/>", function(json){
		status1=json;
	});
	function statusFormatter(value){
		for(var i=0; i<status1.length; i++){
			if (status1[i].value == value) return status1[i].name;
		}
		return value;
	}

	//---------------------------------------------------------------------
	
		var url;	
	
		$(function(){
			$('#tree').tree({
				onClick : function(node) {
					var id =node.id.substr(4);
					if('ual' == id){
						url = '<c:url value='/permissions/allDepartment.tg'/>';
					}else{
						url = '<c:url value='/permissions/findDepartment.tg'/>?id=' + id;
					}
					$('#dt-department').datagrid('options').url = url;
					$('#dt-department').datagrid("reload");
				}
			});
		});
		
		function newItem(){
			$('#myform').form('clear');
			var obj = $('#tree').tree('getSelected');			
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$('#parentId').combotree({
						url:'<c:url value="/permissions/getTreeDepartment.tg"/>',
						valueField:'id',
						textField:'text'
					});
					$('#companyId').combotree({
						url:'<c:url value="/permissions/getTreeCompany.tg"/>',
						valueField:'id',
						textField:'text'
					});					
					url = '<c:url value="/permissions/saveDepartment.tg"/>';
					$('#status').combobox('setValue','1');
					$('#dlg').dialog('setTitle', '填写部门资料').dialog('open');
					return;
				}else{
					url = '<c:url value="/permissions/findDepartment.tg"/>?id=' + id;
				}

				$('#myform').form('submit', {
					url:url,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');
						var tempObj = data.rows[0];
						$('#parentId').combotree({
							url:'<c:url value="/permissions/getTreeDepartment.tg"/>',
							valueField:'id',
							textField:'text'
						});
						$('#parentId').combotree('setText',tempObj.deptName);
						$('#parentId').combotree('setValue',tempObj.id);
						
						$('#companyId').combotree({
							url:'<c:url value="/permissions/getTreeCompany.tg"/>',
							valueField:'id',
							textField:'text'
						});
						
					}
				});
			}else{
				$('#parentId').combotree({
					url:'<c:url value="/permissions/getTreeDepartment.tg"/>',
					valueField:'id',
					textField:'text'
				});
				$('#companyId').combotree({
					url:'<c:url value="/permissions/getTreeCompany.tg"/>',
					valueField:'id',
					textField:'text'
				});				
			}
			$('#status').combobox('setValue','1');
			url = '<c:url value="/permissions/saveDepartment.tg"/>';
			$('#dlg').dialog('setTitle', '填写部门资料').dialog('open');
			
		}
		
		function loadAllTree(){
			url = '<c:url value="/permissions/indexDepartment.tg"/>';
			$('companyForm').form('submit',{
				url:url,
				onSubmit: function(){
					return $('#myform').form('validate');
				},
				success: function(data){
					$("#allTrees").html('');
					$("#allTrees").append(data);
					$("#tree").tree();
				}
			});
		}
		
		function saveItem(){
			$('#myform').form('submit', {
				url:url,
				onSubmit: function(){
					return $('#myform').form('validate');
				},
				success: function(data){
					$('#dlg').dialog('close');
					data=eval('('+data+')');
					if(data.success){
						$.messager.show(
							{
								title:'提示',
								msg:'操作成功！',
								showType:'slide'
							}
						);
						loadAllTrees();
					}
					if(data.error){
						$.messager.alert('提示','操作失败！','error');
					}
				}
			});
		}
		
		//==========================================
		function loadAllTrees(){
			url = '<c:url value="/permissions/listDepartment.tg"/>?';
			 $('#myform').form('submit',{
					url:url,
					onSubmit:function(){return true;},
					success:function(data){
						$("#allTrees").html("");
						$("#allTrees").append($(data));
						$("#tree").tree();
						$('#tree').tree({
							onClick : function(node) {
								var id =node.id.substr(4);
								if('ual' == id){
									url = '<c:url value='/permissions/allDepartment.tg'/>';
								}else{
									url = '<c:url value='/permissions/findDepartment.tg'/>?id=' + id;
								}
								$('#dt-department').datagrid('options').url = url;
								$('#dt-department').datagrid("reload");
							}
						});
						url = '<c:url value='/permissions/allDepartment.tg'/>';
						$('#dt-department').datagrid('options').url = url;
						$('#dt-department').datagrid("reload");
					}
				});
		}
		//==========================================
		
		
		
		function delItem(){
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素不能删除','info');
					return;
				}else{
					url = '<c:url value="/permissions/delDepartment.tg"/>?id=' + id;
				}
				if(confirm('确定要删除？')){
					$('#myform').form('submit', {
						url:url,
						onSubmit: function(){
							return true;
						},
						success: function(data){
							data=eval('('+data+')');
							if(data.success){
								$.messager.show(
									{
										title:'提示',
										msg:'操作成功！',
										showType:'slide'
									}
								);
								loadAllTrees();
							}
							if(data.error){
								$.messager.alert('提示','操作失败！','error');
							}
						}
					});
				}
			}else{
				$.messager.alert('提示','请选择要删除的部门','info');
			}
		}
		function editItem(){
			$('#myform').form('clear');
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素不能修改','info');
					return;
				}else{
					url = '<c:url value="/permissions/findDepartment.tg"/>?id=' + id;
				}
				
				$('#myform').form('submit', {
					url:url,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');
						$('#eid').val(data.rows[0].id);
						$('#deptName').val(data.rows[0].deptName);
						$('#deptCode').val(data.rows[0].deptCode);						
						
						$('#parentId').combotree({
							url:'<c:url value="/permissions/getTreeDepartment.tg"/>',
							valueField:'id',
							textField:'text'
						});						
						
						//$('#parentId').combotree('setValue',data.parentid);
						//$('#parentId').combotree('setText',data.parentName);						
						
						
						$('#companyId').combotree({
							url:'<c:url value="/permissions/getTreeCompany.tg"/>',
							valueField:'id',
							textField:'text'
						});						
						
						//$('#companyId').combotree('setValue',data.companyId);
						//$('#companyId').combotree('setText',data.companyName);
						
						
						$('#orderid').val(data.rows[0].orderId);	
						
						$('#status').combobox('setValue',data.rows[0].status);
					}
				});

				$('#dlg').dialog('setTitle', '修改部门资料').dialog('open');
				url = '<c:url value="/permissions/updateDepartment.tg"/>';
			}else{
				$.messager.alert('提示','请选择要修改的部门','info');
			}
		}
		function lookItem(){
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素不允许查看','info');
					return;
				}else{
					url = '<c:url value="/permissions/findDepartment.tg"/>?id=' + id;
				}
				$('#myform').form('submit', {
					url:url,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');						
						$('#ldeptName').val(data.rows[0].deptName);
						$('#ldeptCode').val(data.rows[0].deptCode);
						$('#lparentId').val(data.rows[0].parentId);
						$('#lcompanyId').val(data.rows[0].companyId);						
						$('#lorderid').val(data.rows[0].orderId);						
		
						if('1' == data.rows[0].status){
							$('#lstatus').val('启用');
						}else{
							$('#lstatus').val('禁用');
						}
					}
				});
				$('#look').dialog('setTitle', '查看部门资料').dialog('open');
			}else{
				$.messager.alert('提示','请选择要查看的部门','info');
			}
		}

	</script>
